﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using RSSReader.MobileClient.ViewModel;
using RSSReader.MobileClient.Model;

namespace RSSReader.MobileClient
{
    public partial class ArticlePage : PhoneApplicationPage
    {
        private ArticleViewModel Articlevm;

        public ArticlePage()
        {
            InitializeComponent();
            Articlevm = new ArticleViewModel();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("Action"))
            {
                NavigationService.RemoveBackEntry();
            }

            if (NavigationContext.QueryString.ContainsKey("LocLink"))
            {
                var asyncMgr = new AsyncDataManager(() =>
                {
                    if (NavigationContext.QueryString.ContainsKey("LocLink"))
                    {
                        if (NavigationContext.QueryString["LocLink"] == "All")
                        {
                            ArticleSelectorOnPage.DataContext = from A in Articlevm.Articles select A;
                            this.PageTitle.Text = "All Feeds";
                        }
                        else
                        {
                            string _currentfeed = Articlevm.Articles[0].FeedOrigin;
                            ArticleSelectorOnPage.DataContext = from A in Articlevm.Articles where A.FeedOrigin == _currentfeed select A;
                            this.PageTitle.Text = _currentfeed;
                        }
                    }
                });

                var asyncMgr2 = new AsyncDataManager(() =>
                {
                    Articlevm.GetFavArticles(asyncMgr);
                });
                asyncMgr.SetExpectation("GotRead");
                asyncMgr.SetExpectation("GotFav");
                if (NavigationContext.QueryString.ContainsKey("LocLink"))
                {
                    if (NavigationContext.QueryString["LocLink"] != "All")
                        Articlevm.GetArticles(NavigationContext.QueryString["LocLink"], asyncMgr2);
                    else
                        Articlevm.GetAllArticles(NavigationContext.QueryString, asyncMgr2);
                    //Articlevm.GetReadArticles();
                    asyncMgr2.SetExpectation("Data2");
                    asyncMgr2.SetExpectation("ImagesDL");
                }
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            if (Articlevm.Articles != null)
            {
                foreach (Article a in Articlevm.Articles)
                {
                    if (a.Link == e.Uri.ToString())
                    {
                        a.isRead = true;
                        a.isReadBrush.Color = Color.FromArgb(50, 50, 50, 255);
                    }
                }
            }
        }
    }
}